/*
 * this object is a wrapper for the File object and simplifies File. 
 * Just do "new", and "open" to open a file for reading.  next() gets the next line.
 * At end, close(). 
 * 
 */
package sound;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 *
 * @author cottonjb
 */
public class FlatFile {

    File file;
    BufferedReader bf;
    String line;
    boolean eof = false;

    public FlatFile() {
    }
/**
 * open() - open the file to read
 * @param filename
 * @return 
 */
    public boolean open(String filename) {
        file = new File(filename);
        if (file.exists()) {
            try {
                bf = new BufferedReader(new FileReader(file));
                line = bf.readLine();
            } catch (Exception e) {
                return false;
            }
            return true;
        }
        return false;
    }

/**
 * next() - read next line and return a String
 * @return 
 */    
    public String next() {
        String line = this.line; // hold this line
        try {
            if (this.line == null) {
                eof = true;
                return null;
            } else {
                this.line = bf.readLine();  // read new line
                if (this.line == null) {
                    eof = true;
                }
            }
        } catch (Exception e) {
            eof = true;
            return null;
        }
        return line; // return local line
    }

/**
 * close() - close the file
 * @return 
 */    
    public boolean close() {
        try {
            bf.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    private static void say(String s) {
        System.out.println(s);
    }
}
